package cc.lzmsh.domain.result;

import cc.lzmsh.enums.AppHttpCodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

/**
 * Author:   msh
 * Date:     2024/9/13 下午4:22
 * Description:
 */

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        result.setMsg(AppHttpCodeEnum.SUCCESS.getMsg());
        return result;
    }

    public static <T> Result<T> success(T data){
        Result<T> result = new Result<>();
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        result.setMsg(AppHttpCodeEnum.SUCCESS.getMsg());
        if (data != null){
            result.setData(data);
        }
        return result;
    }

    public static <T> Result<T> error(){
        Result<T> result = new Result<>();
        result.setMsg(AppHttpCodeEnum.SYSTEM_ERROR.getMsg());
        result.setCode(AppHttpCodeEnum.SYSTEM_ERROR.getCode());
        return result;
    }

    public static <T> Result<T> error(String msg,Integer code){
        Result<T> result = new Result<>();
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }

    public static <T> Result<T> error(AppHttpCodeEnum httpCodeEnum){
        Result<T> result = new Result<>();
        result.setMsg(httpCodeEnum.getMsg());
        result.setCode(httpCodeEnum.getCode());
        return result;
    }
}
